var express = require('express');
var router = express.Router();
var models = require('../models')
var Op = models.Sequelize.Op
var db = require('../models')
/* GET record info */
router.get('/', async function (req, res, next) {
    try {
        if (req.query.userId) {
            var userId = req.query.userId
            var records = await models.Record.findAll({
                where: { [Op.and]: [{ userId }] },
                include: [
                    {
                        model: models.Category
                    },
                    {
                        model: models.Service
                    },
                    {
                        model: models.Customer
                    },
                    {
                        model: models.User
                    },
                    {
                        model: models.Channel
                    },
                    {
                        model: models.Location
                    },
                    {
                        model: models.Audit
                    },
                    {
                        model: models.Bank
                    }
                ]
            })
            res.json({ records })
        } else {
            var records = await models.Record.findAll({
                include: [
                    {
                        model: models.Category
                    },
                    {
                        model: models.Service
                    },
                    {
                        model: models.Customer
                    },
                    {
                        model: models.User
                    },
                    {
                        model: models.Channel
                    },
                    {
                        model: models.Location
                    },
                    {
                        model: models.Audit
                    },
                    {
                        model: models.Bank
                    }
                ]
            })
            res.json({ records })
        }
    } catch (e) {
        console.log(e);
    }
});

// 新建业务信息并插入数据库
router.post('/', async function (req, res, next) {
    const { categoryId, serviceId, customerId, channelId, locationId, auditId, auditUserId,
        eLocation, userId, bankId, acceptedTime, endTime, notes, auditSuggest } = JSON.parse(req.query.data)
    try {
        var record = await models.Record.create({
            categoryId, serviceId, customerId, channelId, locationId, auditId, auditUserId,
            eLocation, userId, bankId, acceptedTime, endTime, notes, auditSuggest
        })
        res.json({ record })
    } catch (e) {
        console.log(e)
    }
})

// 修改信息
router.put('/:id', async function (req, res, next) {
    const id = req.params.id
    const { categoryId, serviceId, customerId, channelId, locationId, auditId, auditUserId,
        eLocation, userId, bankId, acceptedTime, endTime, notes, auditSuggest } = JSON.parse(req.query.data)
    try {
        var record = await models.Record.update({
            categoryId, serviceId, customerId, channelId, locationId, auditId, auditUserId,
            eLocation, userId, bankId, acceptedTime, endTime, notes, auditSuggest
        }, { where: { id } })

        res.json({ record })
    } catch (e) {
        console.log(e)
    }
})

// 修改审核状态
router.put('/auditState/:id', async function (req, res, next) {
    const id = req.params.id
    const { auditId, auditUserId, auditSuggest } = JSON.parse(req.query.data)
    // console.log(auditId);
    const t = await db.sequelize.transaction();
    try {
        var record = await models.Record.update({
            auditId, auditUserId, auditSuggest
        }, { where: { id } })
        await t.commit()
        if (record) {
            res.json({ record })
        }
    } catch (e) {
        console.log(e)
        await t.rollback()
    }
})

// 删除信息
router.delete('/:id', async function (req, res, next) {
    const id = req.params.id
    var record = await models.Record.findByPk(id)
    if (record) {
        record.destroy()
        res.json({ msg: '删除成功' })
    } else {
        res.json({ msg: '不存在该数据' })
    }
})

module.exports = router;
